package com.c.ctgproject.ui.collect

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.c.ctgproject.adapter.guang.PartCanHandlerAdapter
import com.c.ctgproject.adapter.guang.PartDetailActivity
import com.c.ctgproject.base.BaseActivityV2
import com.c.ctgproject.databinding.ActivitySearchPartBinding
import com.c.ctgproject.entity.guang.OutCommonEntity
import com.c.ctgproject.entity.guang.PartOutListEntity
import com.c.ctgproject.http.ApiMethods
import com.c.ctgproject.http.MyObserverHandlerLoading
import com.c.ctgproject.utils.*
import org.jetbrains.anko.startActivity

class SearchPartActivity : BaseActivityV2(), PartCanHandlerAdapter.OnCheckBoxClickedListener{
    private val mBinding by lazy { ActivitySearchPartBinding.inflate(layoutInflater) }
    override fun getLayout()=mBinding.root
    private var search = ""
    private var page = 1
    private val mList = ArrayList<PartOutListEntity.DataBean.ListBean>()
    private val mViewModel:PartHandlerListModelView by lazy { ViewModelProvider(this)[PartHandlerListModelView::class.java] }
    private val mAdapter by lazy { PartCanHandlerAdapter(this, mList,mViewModel,this) }
    override fun event() {
        mBinding.mRecyclerView.adapter = mAdapter
        mBinding.mRecyclerView.layoutManager = LinearLayoutManager(this)
        mViewModel.changeSearchMine(true)
        search=intent.getStringExtra(MyConstants.SEARCH)
        mBinding.mSearchET.setText(search)
        getData()
    }

    override fun click() {
        mBinding.mSearchET.handlerText {
            if (it.isEmpty() || it == "") {
                search = ""
                page = 1
                getData()
            }
        }
        mBinding.mSearchTV.setOnClickListener {
            search = mBinding.mSearchET.text.handlerNull()
            page = 1
            getData()
        }
        mBinding.mRefresh.setOnLoadMoreListener {
            page++
            getData()
        }

        mBinding.mRefresh.setOnRefreshListener {
            page = 1
            getData()
        }
    }
    fun getData(){
        ApiMethods.apiSubscribe(ApiMethods.retrofit.getPartsList( search, page, 20), MyObserverHandlerLoading(this, loading, mBinding.mRefresh) {
            MyLog.d("出库单列表回调:$it")
            val data = getGson().fromJson(it, PartOutListEntity::class.java).data
            data?.list?.let { list ->
                if (page == 1) mList.clear()
                mList.addAll(list)
                mAdapter.notifyDataSetChanged()
                mBinding.mRecyclerView.handlerPosition(page)
            }
        })
    }

    override fun OnItemCheckBoxClicked(position: Int, data: PartOutListEntity.DataBean.ListBean) {
        data.orderId?.let {
            if(it!=0){
                startActivity<PartInWerehouseDetailActivity>(MyConstants.ORDER_ID to it,MyConstants.TITLE_NAME to "散件详情")
            }
        }
    }
}